package com.ingenic.glassassistant.iotc;

import android.content.Context;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

/*
 * LiveDataCodec.java : Decode H264 and AAC Data For IOTC Live
 * setVideoData : 
 *              功能：将H264数据传送到Native,Native解码，格式转换，然后显示。
 *              注意：该函数为不阻塞。也就Java层负责发送数据，Native接收数据并存储，其他的操作在Native其他线程完成。
 * setAudioData:
 *              功能：将AAC数据传送到Native，Native只负责解码。
 *	        注意：该函数为带超时阻塞。也就是Java曾负责发送数据，并在Java线程完成解码功能，解码成功或超时返回。
 *			          所以Java层在函数返回时可以直接播放音频数据。
 * */

public class LiveDataCodec {
	static {
		System.loadLibrary("iotclive_jni");
	}
	private static final String TAG = "LiveDataDecode";
	private final boolean DEBUG = false;

	// used by native
	private int mNativeContext = 0;
	private int mNativeWindow = 0;

	private native final void native_start();

	private native final void native_stop();

	private native final void native_set_surface(Surface surfaace);

	private native final int native_set_video_data(Object buffer);

	private native final void native_set_audio_data(Object inBuffer,
			Object outBuffer);

	private native final void native_set_audio_pcm(Object inBuffer,
			Object outBuffer, int outCapacity);

	private native final int native_set_outputfile(String fileName);

	private native final int native_stop_record();

	public LiveDataCodec() {
	}

	public void start() {
		if (DEBUG)
			Log.i(TAG, "start");
		native_start();
	}

	public void close() {
		if (DEBUG)
			Log.i(TAG, "close");
		native_stop();
	}

	public void setSurface(SurfaceHolder holder) {
		if (DEBUG)
			Log.i(TAG, "setSurface");
		native_set_surface(holder.getSurface());
	}

	public int setVideoData(Object buffer) {
		return native_set_video_data(buffer);
	}

	public void setAudioData(Object inBuffer, Object outBuffer) {
		native_set_audio_data(inBuffer, outBuffer);
	}

	public void setAudioPCM(Object inBuffer, Object outBuffer, int outCapacity) {
		native_set_audio_pcm(inBuffer, outBuffer, outCapacity);
	}

	public int setOutputFile(String fileName) {
		return native_set_outputfile(fileName);
	}

	public int stopRecord() {
		return native_stop_record();
	}
}